package cn.kzgg.user.service.impl;

import cn.kzgg.basic.constants.BaseConstant;
import cn.kzgg.basic.dto.RegisterDto;
import cn.kzgg.basic.exception.BusinessException;
import cn.kzgg.basic.service.impl.BaseServiceImpl;
import cn.kzgg.basic.util.MD5Utils;
import cn.kzgg.basic.util.StrUtils;
import cn.kzgg.user.domain.Logininfo;
import cn.kzgg.user.domain.User;
import cn.kzgg.user.mapper.LogininfoMapper;
import cn.kzgg.user.mapper.UserMapper;
import cn.kzgg.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author KZGG
 * @since 2022-06-16
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private UserMapper userMapper;


    @Override
    public void registerPhone(RegisterDto dto) {

        String phone = dto.getPhone();
        String smsCode = dto.getSmsCode();
        String password = dto.getPassword();
        String passwordRepeat = dto.getPasswordRepeat();
        //1.校验
        //1.1空校验,空的话提示不能为空
        if (StringUtils.isEmpty(smsCode)
                || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(passwordRepeat)) {
            throw new BusinessException("参数不能为空");
        }
        //1.2校验两次输入的密码是否一致，不一致提示，一致继续往下走
        if (!password.equals(passwordRepeat)) {
            throw new BusinessException("哥们儿，两次密码不一样");
        }
        //2获取到短信验证码判断
        //根据是手机号拼接key
        String key = BaseConstant.RegisterConstant.REGISTER_SMS_PREFIX + phone;
        Object smsTmp = redisTemplate.opsForValue().get(key);
        //2.1拿不到，提示验证码过期。重新获取
        if (smsCode == null) {
            throw new BusinessException("验证码已过期请重新获取");
        }
        //2.2拿到了，判断是否正确，不正确的话提示，正确继续往下走
        if (!smsCode.equals(smsTmp.toString().split(":")[0])) {
            throw new BusinessException("密码错误请重试");
        }
        //3.保存
        User user = dto2User(dto);
        Logininfo logininfo = user2Logininfo(user);
        logininfoMapper.save(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.save(user);


    }

    /**
     * 根据LogininfoId获取当前登录人
     * @param id
     * @return
     */
    @Override
    public User loadByLogininfoId(Long id) {
        return userMapper.loadByLogininfoId(id);
    }





    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user, logininfo);
        logininfo.setType(1);
        return logininfo;
    }

    private User dto2User(RegisterDto dto) {
        User user = new User();
        user.setUsername(dto.getPhone());
        user.setPhone(dto.getPhone());
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = MD5Utils.encrypByMd5(dto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);
        return user;
    }


    @Override
    public void update(User user) {
        super.update(user);
    }

    @Override
    public void removeById(Long id) {
        super.removeById(id);
    }
}
